//
//  RifInterventoViewController.m
//  InterventixApp
//
//  Created by Diego Falcinelli on 01/06/13.
//
//

#import "RifInterventoViewController.h"

@interface RifInterventoViewController ()

@end

@implementation RifInterventoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.array = [NSMutableArray arrayWithCapacity:0];
    [self.array addObject:@"Riferimento Fattura"];
    [self.array addObject:@"Riferimento Scontrino"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction) indietro{
    
	[[self navigationController] popViewControllerAnimated:YES];
}

// TABELLA

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if([self.intervento.idintervento intValue]<0)return @"Riferimenti Nuovo Intervento";
	return [NSString stringWithFormat: @"Riferimenti Intervento %@",self.intervento.idintervento];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    [cell setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:1]];
    
    NSString *name = [self.array objectAtIndex:indexPath.row];
    if([name isEqual:@"Riferimento Fattura"] && self.intervento.rif_fattura != nil && ![self.intervento.rif_fattura isEqual:[NSNull null]]){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = self.intervento.rif_fattura;
    }
    else if([name isEqual:@"Riferimento Scontrino"] && self.intervento.rif_scontrino != nil && ![self.intervento.rif_scontrino isEqual:[NSNull null]]){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = self.intervento.rif_scontrino;
    }
    else{
        cell.textLabel.text = name;
    }
    
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}


// CellHeight
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 50;
}

// CLICK
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *name = [self.array objectAtIndex:indexPath.row];
    self.nameSelect = name;
    
    if([name isEqual:@"Riferimento Fattura"]){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Fattura"
                                                              message:@"Riferimento a una fattura per l'intervento" delegate:self cancelButtonTitle:@"Annulla" otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [myAlertView textFieldAtIndex:0].font =[UIFont systemFontOfSize:12];
        if(![self.intervento.prodotto isEqual:[NSNull null]]){
            [myAlertView textFieldAtIndex:0].text = [NSString stringWithFormat:@"%@",self.intervento.rif_fattura];
        }
        [myAlertView show];
    }
    else if([name isEqual:@"Riferimento Scontrino"]){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Scontrino"
                                                              message:@"Riferimento a scontrino per l'intervento" delegate:self cancelButtonTitle:@"Annulla" otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [myAlertView textFieldAtIndex:0].font =[UIFont systemFontOfSize:12];
        if(![self.intervento.prodotto isEqual:[NSNull null]]){
            [myAlertView textFieldAtIndex:0].text = [NSString stringWithFormat:@"%@",self.intervento.rif_scontrino];
        }
        [myAlertView show];
    }
    else{
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1){
        NSString *name = self.nameSelect;
        
        NSString *value = [[alertView textFieldAtIndex:0]text];
        
        if([name isEqual:@"Riferimento Fattura"]){
            self.intervento.rif_fattura = value;
        }
        else if([name isEqual:@"Riferimento Scontrino"]){
            self.intervento.rif_scontrino = value;
        }
        else{
        }
        [self.table reloadData];
    }
}


@end
